﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;


public partial class client_Work_Orders : System.Web.UI.Page
{
    private int workorderID;
    protected void Page_Load(object sender, EventArgs e)
    {
        //String machineID = Request.QueryString["id"];
        //CreateConn objcon = new CreateConn();
        //string query = "SELECT siteName, machineName, photo" +
        //        " FROM  Sites INNER JOIN" +
        //        " Machines ON Sites.site_id = Machines.site_id" +
        //        " WHERE (Machines.machine_id = " + machineID + ")";
        //DataTable dt = objcon.SQLConnDataTable(query);

        //sitename.Text = dt.Rows[0]["siteName"].ToString();
        //machinename.Text = dt.Rows[0]["machineName"].ToString();
        if (!IsPostBack)
        {
            sitename.Text = Session["sitename"].ToString();
            machinename.Text = Session["machinename"].ToString();
            string machineID = Session["machineID"].ToString();
            this.machineid.Text = machineID;
            if (this.LoadWorkOrder(machineID))
            {
                //   Component1.Visible = true;
                //  Table_Action.Visible = true;
                PlaceHolder1.Visible = true;
                mode_switch(0);
            }
            else
            {
                MessagePlaceHolder.Visible = true;
                wo_message.Text = "No work order exist";
            }
        }
    }

    //protected void Button1_Click(object sender, EventArgs e) {
    //    String frm_date_submitted = WODateTime.Text.ToString();
    //    String frm_description = WODescription.Text.ToString();
    //    String frm_workorder_reference_no = WONumber.Text.ToString();
    //    String frm_machine_id = machineid.Text.ToString();

    //    CreateConn objcon = new CreateConn();
    //    string query = "INSERT INTO WorkOrders (workorder_reference_no, machine_id, description, date_submitted)"
    //           + " values ('" + frm_workorder_reference_no + "'," + frm_machine_id + ",'" + frm_description + "','" + frm_date_submitted + "')";

    //    objcon.SQLCmd(query);

    //    wo_message.Text = "Work Order: " + WONumber.Text.ToString() + " Successfully created";
    //    WODescription.Text = "";
    //    WONumber.Text = "";
    //}
    protected void submitButton_Click(object sender, EventArgs e)
    {

        String frm_date_submitted = WODateTime.Text.ToString();
        String frm_description = WODescription.Text.ToString();
        String frm_workorder_reference_no = WONumber.Text.ToString();
        String frm_machine_id = machineid.Text.ToString();
        string status = status_checkbox.Checked ? "1" : "0";

        CreateConn objcon = new CreateConn();
        string query = "INSERT INTO WorkOrders (workorder_reference_no, machine_id, description,action, date_submitted,status)"
               + " values ('" + frm_workorder_reference_no + "'," + frm_machine_id + ",'" + frm_description + "','" + action_tb.Text + "',convert(datetime,'" + frm_date_submitted + "', 103),'" + status + "')";
        try
        {
            objcon.SQLConnDataTable(query);
            LoadWorkOrder(frm_machine_id);
        }
        catch (Exception e_insert)
        {
            Response.Redirect(e_insert.ToString());
        }
        mode_switch(0);
        // EditButton.Visible = true;
        MessagePlaceHolder.Visible = true;
        wo_message.Text = "Work Order: " + WONumber.Text.ToString() + " Successfully created";
        //WODateTime.Enabled = false;
        //WONumber.Enabled = false; ;
        //WODescription.Enabled = false; ;
        //status_checkbox.Enabled = false;
        //submitButton.Visible = false;
        //WODescription.Text = "";
        //WONumber.Text = "";
    }
    protected bool LoadWorkOrder(string machineID)
    {
        string guery = "SELECT TOP 1 * FROM WorkOrders WHERE machine_id='" + Int32.Parse(machineID) + "' order by date_submitted desc";
        CreateConn objcon = new CreateConn();
        try
        {
            DataTable dt = objcon.SQLConnDataTable(guery);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string workorderNo = dr["workorder_reference_no"].ToString();
                    string description = dr["description"].ToString();
                    string date_submitted = dr["date_submitted"].ToString();
                    string status = dr["status"].ToString();
                    string action = dr["action"].ToString();
                    this.wo_lb.Text = workorderNo;
                    this.date_lb.Text = date_submitted;
                    this.desc_lb.Text = description;
                    this.action_lb.Text = action;
                    Statuslabel.Text = status == "1" ? "Completed" : "OutStanding";
                    WONumber.Text = workorderNo;
                    Statuslabel.ForeColor = System.Drawing.Color.FromName(ConvertToColor(status));

                    //                string prevCol = ConvertToColor(status);

                    //    CurrentLabel.ForeColor = System.Drawing.Color.FromName(prevCol);
                    //  PreviousLabel.NavigateUrl = "Work_Order.aspx?id=" + machineID + "&action=next";

                }
                return true;
            }
        }
        catch (Exception e)
        {

        }
        return false;
    }
    protected string ConvertToColor(string col)
    {

        if (col == "0")
        {
            col = "#FF0000";
        }
        else
        {
            col = "#00FF00";
        }
        return col;
    }
    protected void defaultSearch_clicked(object sender, EventArgs e)
    {
        String keyword = keyword_TB.Text;
        if (keyword == "")
        {
            Response.Write("<script language=javascript> alert('Must Input W/O No.')</script>");
        }
        else
        {
            string query = "SELECT * FROM WorkOrders WHERE workorder_reference_no='" + keyword + "'";//should check 
            //machine belongs to the client;
            CreateConn obj = new CreateConn();
            DataTable dt = obj.SQLConnDataTable(query);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    mode_switch(0);
                    int machineID = Int16.Parse(dr["machine_id"].ToString());
                    matchMachine(machineID);
                    workorderID = Int32.Parse(dr["workorder_id"].ToString());
                    LoadWO(workorderID);
                    //WODateTime.Text = dr["date_submitted"].ToString();
                    //WODescription.Text = dr["description"].ToString();
                    //status_checkbox.Checked = dr["status"].ToString() == "1" ? true : false;
                    //WONumber.Text = dr["workorder_reference_no"].ToString();

                }
            }
            else
            {
                MessagePlaceHolder.Visible = true;
                wo_message.ForeColor = System.Drawing.Color.Red;
                wo_message.Text = "No match was found";
            }

        }


    }
    protected void matchMachine(int machineID)
    {
        string query = "select machineName,siteName, s.site_id From Machines m join"
            + " Sites s on m.site_id = s.site_id Where machine_id='" + machineID + "'";
        CreateConn objcon = new CreateConn();
        try
        {
            DataTable dt = objcon.SQLConnDataTable(query);
            foreach (DataRow dr in dt.Rows)
            {
                sitename.Text = dr["siteName"].ToString();
                machineid.Text = machineID.ToString();
                machinename.Text = dr["machineName"].ToString();
            }
        }
        catch (Exception e)
        {
            Response.Write(e.ToString());
        }
    }
    protected void preClicked(object sender, EventArgs e)
    {
        mode_switch(0);
        workorderID = getworkorderID(WONumber.Text);

        string query = "SELECT * FROM WorkOrders WHERE machine_id='" + Int32.Parse(machineid.Text) + "' and workorder_reference_no!='" + WONumber.Text + "' and workorder_id <'" + workorderID + "' order by date_submitted desc";
        CreateConn cc = new CreateConn();
        DataTable dt = cc.SQLConnDataTable(query);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string worderorderNo = dr["workorder_id"].ToString();
                    LoadWO(Int32.Parse(worderorderNo));

                    //string workorderRef = dr["workorder_reference_no"].ToString();
                    //WONumber.Text = workorderRef;
                    //WODateTime.Text = dr["date_submitted"].ToString();
                    //status_checkbox.Checked = dr["status"].ToString() == "1" ? true : false;
                    //WODescription.Text = dr["description"].ToString();

                    wo_message.Text = "";

                    break;
                }
            }
            else
            {
                //Response.Write("<script language=javascript> alert('No Next workorder exists')</script>");
                MessagePlaceHolder.Visible = true;
                wo_message.Text = "No previous work order exists";
                wo_message.ForeColor = System.Drawing.Color.Red;
                LoadWO(workorderID);
            }
        }
    }
    protected void nextwoClicked(object sender, EventArgs e)
    {
        mode_switch(0);
        workorderID = getworkorderID(WONumber.Text);

        string query = "SELECT * FROM WorkOrders WHERE machine_id='" + Int32.Parse(machineid.Text) + "' and workorder_reference_no!='" + WONumber.Text + "' and workorder_id >'" + workorderID + "' order by date_submitted asc";
        CreateConn cc = new CreateConn();
        DataTable dt = cc.SQLConnDataTable(query);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string worderorderNo = dr["workorder_id"].ToString();
                    LoadWO(Int32.Parse(worderorderNo));
                    //string workorderRef = dr["workorder_reference_no"].ToString();
                    //WONumber.Text = workorderRef;
                    //WODateTime.Text = dr["date_submitted"].ToString();
                    //status_checkbox.Checked = dr["status"].ToString() == "1" ? true : false;
                    //WODescription.Text = dr["description"].ToString();

                    wo_message.Text = "";
                    break;
                }
            }
            else
            {
                //  Response.Write("<script language=javascript> alert('No previous workorder exists')</script>");
                MessagePlaceHolder.Visible = true;
                wo_message.Text = "No next work order exists";
                wo_message.ForeColor = System.Drawing.Color.Red;

                LoadWO(workorderID);
            }
        }


    }

    protected int getworkorderID(string referenceNo)
    {
        string query = "SELECT workorder_id FROM WorkOrders WHERE workorder_reference_no='" + WONumber.Text + "'";
        CreateConn ccobj = new CreateConn();
        DataTable dt = ccobj.SQLConnDataTable(query);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                return this.workorderID = Int32.Parse(dr["workorder_id"].ToString());
                //Session["workorderid"] = workorderID;
            }
        }
        return 0;
    }
    protected void createbuttonclicked(object sender, EventArgs e)
    {
        mode_switch(2);
        WODateTime.Text = "";
        WONumber.Text = "";
        WODescription.Text = "";


    }
    protected void UpdateButton_Click(object sender, EventArgs e)
    {

        workorderID = (int)Session["workorderid"];
        string status = status_checkbox.Checked ? "1" : "0";
        string query = "Update WorkOrders Set workorder_reference_no='" + WONumber.Text + "',action='" + action_tb.Text + "', description='"
           + WODescription.Text + "',status = '" + status + "',date_submitted=convert(datetime,'" + WODateTime.Text + "', 101) WHERE "
           + "workorder_id='" + workorderID + "'";
        CreateConn cc = new CreateConn();
        try
        {
            cc.SQLConnDataTable(query);
            mode_switch(0);
            this.LoadWO(workorderID);

        }
        catch (Exception e2)
        {
            Response.Write(e2.ToString());
        }

        //WODateTime.Enabled = false;
        //WONumber.Enabled = false; ;
        //WODescription.Enabled = false; ;
        //status_checkbox.Enabled = false;
        //  UpdateButton.Visible = false;

    }
    protected void LoadWO(int id)
    {
        string guery = "SELECT * FROM WorkOrders WHERE workorder_id='" + id + "'";
        CreateConn objcon = new CreateConn();
        try
        {
            DataTable dt = objcon.SQLConnDataTable(guery);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string workorderNo = dr["workorder_reference_no"].ToString();
                    string description = dr["description"].ToString();
                    string date_submitted = dr["date_submitted"].ToString();
                    string status = dr["status"].ToString();
                    string action = dr["action"].ToString();
                    this.wo_lb.Text = workorderNo;
                    this.date_lb.Text = date_submitted;
                    this.desc_lb.Text = description;
                    this.action_lb.Text = action;
                    Statuslabel.Text = status == "1" ? "Completed" : "OutStanding";
                    WONumber.Text = workorderNo;
                    Statuslabel.ForeColor = System.Drawing.Color.FromName(ConvertToColor(status));

                    //                string prevCol = ConvertToColor(status);

                    //    CurrentLabel.ForeColor = System.Drawing.Color.FromName(prevCol);
                    //  PreviousLabel.NavigateUrl = "Work_Order.aspx?id=" + machineID + "&action=next";

                }

            }
        }
        catch (Exception e)
        {

        }

    }
    protected void mode_switch(int mode)
    {
        switch (mode)
        {
            case 0:
                WODateTime.Visible = false;
                WONumber.Visible = false; ;
                WODescription.Visible = false; ;
                status_checkbox.Visible = false;
                UpdateButton.Visible = false;
                action_tb.Visible = false;
                wo_lb.Visible = true;
                date_lb.Visible = true;
                desc_lb.Visible = true;
                action_lb.Visible = true;
                Statuslabel.Visible = true;
                EditButton.Visible = true;
                submitButton.Visible = false;
                break;//display
            case 1:
                WODateTime.Visible = true;
                WONumber.Visible = true;
                action_tb.Visible = true;
                WODescription.Visible = true; ;
                status_checkbox.Visible = true;
                UpdateButton.Visible = true;
                wo_lb.Visible = false;
                date_lb.Visible = false;
                desc_lb.Visible = false;
                action_lb.Visible = false;
                Statuslabel.Visible = false;
                WODateTime.Text = date_lb.Text;
                WONumber.Text = wo_lb.Text;
                WODescription.Text = desc_lb.Text;
                EditButton.Visible = false;
                UpdateButton.Visible = true;
                submitButton.Visible = false;
                break;//edit mode

            case 2:
                Statuslabel.Visible = false;
                action_tb.Visible = true;

                WODateTime.Visible = true;
                WONumber.Visible = true; ;
                WODescription.Visible = true; ;
                status_checkbox.Visible = true;
                UpdateButton.Visible = true;
                wo_lb.Visible = false;
                date_lb.Visible = false;
                desc_lb.Visible = false;
                action_lb.Visible = false;
                WODateTime.Text = date_lb.Text;
                WONumber.Text = wo_lb.Text;
                WODescription.Text = desc_lb.Text;
                UpdateButton.Visible = false;
                EditButton.Visible = false;
                submitButton.Visible = true;
                break;//create new workorder

            //case 3:
            //  Statuslabel.Visible = false;

            //  WODateTime.Visible = false;
            //  WONumber.Visible = false; ;
            //  WODescription.Visible = false; ;
            //  status_checkbox.Visible = false;
            //  UpdateButton.Visible = false;
            //  wo_lb.Visible = true;
            //  date_lb.Visible = true;
            //  desc_lb.Visible = true;
            //  action_lb.Visible = true;
            //  EditButton.Visible = true;
            //  submitButton.Visible = false;
            //  break;//search
            default: ; break;//no data
        }
    }
    protected void EditButton_click(object sender, EventArgs e)
    {
        mode_switch(1);
        status_checkbox.Checked = Statuslabel.Text == "Completed" ? true : false;
        string query = "SELECT workorder_id FROM WorkOrders WHERE workorder_reference_no='" + WONumber.Text + "'";
        CreateConn ccobj = new CreateConn();
        DataTable dt = ccobj.SQLConnDataTable(query);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                this.workorderID = Int32.Parse(dr["workorder_id"].ToString());
                Session["workorderid"] = workorderID;
            }
        }
    }
    protected void searchbuttonClicked(object sender, EventArgs e)
    {
        String clientID = Request.QueryString["client_id"];
        Session["clientid"] = clientID;

        Response.Redirect("Search_Workorder.aspx");
    }

}
